﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data.Common;

namespace YourMoney
{
    /// <summary>
    /// Форма для добавления новой сущности(дохода/расхода)
    /// </summary>
    public partial class AddForm : Form
    {
        List<TypeOfEssence> essenceTypeList;
        private SQLiteDriver drv;
        public AddForm()
        {
            essenceTypeList = new List<TypeOfEssence>();
            drv = SQLiteDriver.GetDrv("Money.db3");
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            sumTbx.Text = "0";
            commentTbx.Text = "none";
            KindCbxUpdate();
            kindCbx.SelectedItem = kindCbx.Items[0];
            treeViewFilling();
            typeTreeView.ExpandAll();
            typeTreeView.SelectedNode = typeTreeView.Nodes[0];

        }

        private void KindCbxUpdate()
        {
            
            kindCbx.Items.Clear();
            foreach (TypeOfEssence type in StaticMembers.KindSelect())
            {
                kindCbx.Items.Add(type);
            }
            
        }
        /// <summary>
        /// Заполнение treeview значениями типов сущностей
        /// </summary>
        private void treeViewFilling()
        {
            typeTreeView.Nodes.Clear();
            TreeNode node = new TreeNode();
            node.Text = ((TypeOfEssence)kindCbx.SelectedItem).Name;
            node.Tag = kindCbx.SelectedItem;
            List<TreeNode> rootsList = new List<TreeNode>();
            rootsList.Add(node);
            for (int i = 0; i < rootsList.Count; i++)
            {
                StaticMembers.SetEssenceTree(rootsList[i]);
                foreach (TreeNode node1 in rootsList[i].Nodes) 
                {
                    typeTreeView.Nodes.Add(node1);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int typeId = ((TypeOfEssence)typeTreeView.SelectedNode.Tag).ID;
                int sum = Convert.ToInt32(sumTbx.Text);
                int date = (int)((datePicker.Value - new DateTime(1970, 1, 1)).TotalSeconds);
                string comment = commentTbx.Text;
                Essence newEsence = new Essence
                    (typeId, sum, date, comment);
                string command =
                    @"INSERT INTO 'Essence' ('type_id','size','date','comment') 
                VALUES ('" + newEsence.TypeID + "','" + newEsence.Size + "','" + newEsence.Date + "','" + newEsence.Comment + "');";
                drv.NonExecuteQuery(command);
                MessageBox.Show("Строка успешно добавлена");
                this.Close();
                drv.Close();
            }
            catch (SQLiteException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void kindCbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            treeViewFilling();
            typeTreeView.ExpandAll();
            
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }
    }
}
